<?php
namespace app\api\model;

use think\Model;
use think\db;

class Ticket extends Model
{

    /**
     * 返回所有未失效的优惠券
     * @return mixed
     */
    public function index()
    {
        error_reporting(0);
        self::checkstatus();
        $ticket = new Ticket();
        $data = $ticket->where('status',0)->select();
        return $data;
    }

    /**
     * 更改优惠券的状态
     */
    public function checkstatus()
    {
        $data = Db::name('ticket')->where('status',0)->field('id,stoptime')->select();
        foreach ($data as $info)
        {
            $infotime = $info['stoptime'];
            if(time()> strtotime($infotime))
            {
                $aaa = Db::name('ticket')->where('id',$info['id'])->update(['status'=>1]);
            }
        }
    }

    /**
     * 用户抢优惠券
     * @param $ticketid   优惠券id
     * @param $userid     用户id
     */
    public function rob($ticketid,$userid)
    {
        error_reporting(0);
        self::checkstatus();
        $tickets = Db::name('ticket')->where('id',$ticketid)->field('surplus')->select();
        if($tickets <= 0)
        {
            return 0;
        }
        else
        {
            //开启事务
            Db::startTrans();
            try {
                $query = Db::name('ticket')->where('id',$ticketid)->setDec('surplus');
                $query2 = Db::name('userticket')->insert(['ticketid' =>$ticketid, 'userid' => $userid, 'status'=>0]);
                // 判断是否修改成功
                if (!$query || !$query2) {
                    throw new \Exception("操作失败");
                }
                // 提交事务
                Db::commit();
                return 1;                  //全部执行成功之后的返回值
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return 0;                //执行失败时的返回值
            }
        }
    }

    /**
     * 用户查询自己的优惠券
     */
    public function mytickets($userid)
    {
        error_reporting(0);
        $ticketsid = Db::name('userticket')->field('id,ticketid')->group('ticketid')->where('userid',$userid)->select();
        if(count($ticketsid) == 0)
        {
            return 0;
        }
        else
        {
            //查询用户的优惠券是否到期
            foreach($ticketsid as $ticketid)
            {

                $a = Db::name('ticket')->field('stoptime')->where('id',$ticketid['ticketid'])->select();
                $infotime = $a[0]['stoptime'];
                //如果优惠券到期
                if(time()> strtotime($infotime))
                {
                    $aaa = Db::name('ticket')->where('id',$ticketid['ticketid'])->update(['status'=>1]);
                    $bbb = Db::name('userticket')->where('ticketid',$ticketid['ticketid'])->update(['status'=>1]);
                }
            }
            $tickets = Db::name('userticket')
                ->alias('u')
                ->join('ticket t','u.ticketid=t.id','left')
                ->field('u.status,money,min,issueid,comment')
                ->where('userid',$userid)
                ->select();
            return  $tickets;
        }
    }




}